package com.jxt.myjxtmanager.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;

/**
 * 显示网页的activity
 */
@SuppressLint("JavascriptInterface")
public class WebViewtActivity extends BaseActivity {
    private Context mContext;
    private WebView webView;
    private ProgressBar progressBar;
    private String headerTitle;
    String url, title;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        outState.putString("url", url);
        outState.putString("title", title);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        mContext = this;
        progressBar = (ProgressBar) findViewById(R.id.event_progress);
        webView = (WebView) findViewById(R.id.today_event_webview);

        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");

        initView(savedInstanceState);
    }

    @Override
    public void onClickLeftMenu() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initView(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            url = savedInstanceState.getString("url");
            title = savedInstanceState.getString("title");
        }
        setToolbarTitle(title);
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        headerTitle = title;

        WebSettings webSet = webView.getSettings();
        webSet.setJavaScriptEnabled(true);
//        webSet.setJavaScriptCanOpenWindowsAutomatically(true);
        webSet.setUseWideViewPort(true);//关键点
        webSet.setBuiltInZoomControls(true); // 设置显示缩放按钮
        webSet.setSupportZoom(true); // 支持缩放
//        webSet.setLoadWithOverviewMode(true);

//        DisplayMetrics metrics = new DisplayMetrics();
//        getWindowManager().getDefaultDisplay().getMetrics(metrics);
//        int mDensity = metrics.densityDpi;
//        if (mDensity == 240) {
//            webSet.setDefaultZoom(WebSettings.ZoomDensity.FAR);
//        } else if (mDensity == 160) {
//            webSet.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
//        } else if(mDensity == 120) {
//            webSet.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
//        }else if(mDensity == DisplayMetrics.DENSITY_XHIGH){
//            webSet.setDefaultZoom(WebSettings.ZoomDensity.FAR);
//        }else if (mDensity == DisplayMetrics.DENSITY_TV){
//            webSet.setDefaultZoom(WebSettings.ZoomDensity.FAR);
//        }else{
//            webSet.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
//        }

/**
 * 用WebView显示图片，可使用这个参数 设置网页布局类型：
 * 1、LayoutAlgorithm.NARROW_COLUMNS ：适应内容大小
 * 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
 */
//        webSet.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webView.loadUrl(url);
        webView.requestFocus();
        webView.addJavascriptInterface(this, "slkj");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // Uri uri = Uri.parse(url);
                // Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                // //intent.setComponent(new
                // ComponentName("com.android.browser",
                // "com.android.browser.BrowserActivity"));
                // // intent.setClassName("com.android.browser",
                // "com.android.browser.BrowserActivity"); //这是打开系统浏览器
                // startActivity(intent);
                view.loadUrl(url);
                return true;
            }
        });
        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
                    webView.goBack();
                    return true;
                }
                return false;
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                // Activity和Webview根据加载程度决定进度条的进度大小
                // 当加载到100%的时候 进度条自动消失
                progressBar.setProgress(progress);
                if (progress == 100) {
                    progressBar.setVisibility(View.GONE);
                } else {
                    if (progressBar.getVisibility() == View.GONE)
                        progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(progress);
                }
                super.onProgressChanged(view, progress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                // TODO Auto-generated method stub
                super.onReceivedTitle(view, title);

                if (webView.canGoBack())
                    setToolbarTitle(TextUtils.isEmpty(title) ? headerTitle
                            : title);
            }

        });
    }

    public boolean onKeyDown(int keyCoder, KeyEvent event) {
        super.onKeyDown(keyCoder, event);
        if (webView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK) {
            webView.goBack(); // goBack()表示返回webView的上一页面
        }
        return true;
    }
}
